from apps.blueprints.user.models import User
from exts import db
from flask import redirect, render_template, request, url_for

from . import article_bp
from .models import Articles


@article_bp.route('/all') # 显示文章列表
def all_article():
    articles = Articles.query.all()
    return render_template('article/all.html', articles=articles)

@article_bp.route('/publish', methods=['GET', 'POST']) # 发布文章
def add_article():
    if request.method == 'POST':
        title = request.form.get('title') # 文章标题
        content = request.form.get('content') # 文章内容
        uid = request.form.get('uid') # 用户ID
        
        # 添加文章到数据库
        article = Articles(title=title, content=content,user_id=uid) # 
        db.session.add(article)
        db.session.commit()
        return redirect(url_for('article.add_article'))
    else:
        users = User.query.filter(User.isdelete == False).all()
        return render_template('article/add_article.html', users=users)

@article_bp.route('/all_author') # 根据作者找文章
def all_author():
    id = request.args.get('id')
    user = User.query.get(id) # 找到id为多少的用户，在html中访问其对应的文章
    
    return render_template('article/all_author.html', user=user)
